/*******************************************************************************
 * Copyright (C) 2020, Huada Semiconductor Co., Ltd. All rights reserved.
 *
 * This software component is licensed by HDSC under BSD 3-Clause license
 * (the "License"); You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at:
 *                    opensource.org/licenses/BSD-3-Clause
 */
/******************************************************************************/
/** \file main.c
 **
 ** \brief The example of SPI four wire polling tx and rx function
 **
 **   - 2021-04-16  CDT  First version for Device Driver Library of SPI.
 **
 ******************************************************************************/

/*******************************************************************************
 * Include files
 ******************************************************************************/
#include "hc32_ddl.h"
#include "w5500/w5500.h"
#include <stdint.h>
#include <rthw.h>
#include <rtthread.h>

/*******************************************************************************
 * Local type definitions ('typedef')
 ******************************************************************************/

/*******************************************************************************
 * Local pre-processor symbols/macros ('#define')
 ******************************************************************************/
/* SPI_SCK Port/Pin definition */
#define SPI_SCK_PORT                    (PortA)
#define SPI_SCK_PIN                     (Pin11)
#define SPI_SCK_FUNC                    (Func_Spi1_Sck)

/* SPI_NSS Port/Pin definition */
#define SPI_NSS_PORT                    (PortA)
#define SPI_NSS_PIN                     (Pin12)
#define SPI_NSS_FUNC                    (Func_Spi1_Nss0)

/* SPI_MOSI Port/Pin definition */
#define SPI_MOSI_PORT                   (PortA)
#define SPI_MOSI_PIN                    (Pin09)
#define SPI_MOSI_FUNC                   (Func_Spi1_Mosi)

/* SPI_MISO Port/Pin definition */
#define SPI_MISO_PORT                   (PortA)
#define SPI_MISO_PIN                    (Pin10)
#define SPI_MISO_FUNC                   (Func_Spi3_Miso)

/* SPI unit and clock definition */
#define SPI_UNIT                        (M4_SPI1)
#define SPI_UNIT_CLOCK                  (PWC_FCG1_PERIPH_SPI1)

/* Choose SPI master or slave mode */
#define SPI_MASTER_MODE
//#define SPI_SLAVE_MODE

/*******************************************************************************
 * Global variable definitions (declared in header file with 'extern')
 ******************************************************************************/

/*******************************************************************************
 * Local function prototypes ('static')
 ******************************************************************************/

/*******************************************************************************
 * Local variable definitions ('static')
 ******************************************************************************/
 
/*******************************************************************************
 * Function implementation - global ('extern') and local ('static')
 ******************************************************************************/
/**
 *******************************************************************************
 ** \brief Configure SPI peripheral function
 **
 ** \param [in] None
 **
 ** \retval None
 **
 ******************************************************************************/
static void LED_Config(void)
{ 
    stc_port_init_t stcPortInit;
    /* configuration structure initialization */
 
    MEM_ZERO_STRUCT(stcPortInit);
    
    
    stcPortInit.enPinMode = Pin_Mode_Out;
    stcPortInit.enExInt = Enable;
    stcPortInit.enPullUp = Enable;
    
    
    PORT_Unlock();
    M4_PORT->PSPCR= (0x1Fu &~(1<<3));
    PORT_Lock();
   

    PORT_Init(PortA, Pin15, &stcPortInit);
    
    PORT_SetPortData(PortA,Pin15);
     
    
    
}

/**
 *******************************************************************************
 ** \brief  main function for four wire SPI polling tx and rx function
 **
 ** \param [in]  None
 **
 ** \retval int32_t Return value, if needed
 **
 ******************************************************************************/
int tt = 0;
int32_t main(void)
{
 
    /* BSP initialization */
    
  LED_Config();
    /* Configure SPI */
   
    /* Get tx buffer length */


    while (1)
    {
      /* Wait key trigger in master mode */
      
      rt_thread_delay(100);
      
      
      tt++;
      PORT_Toggle(PortA,Pin15);
      
    }
}



/*******************************************************************************
 * EOF (not truncated)
 ******************************************************************************/
